﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace FallingSand
{
    public partial class GamePage : PhoneApplicationPage
    {
        UIElementRenderer elementRenderer;

        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        private int _size = 2;
        private int _state = 1;
        private static Grid _grid;

        public GamePage()
        {
            InitializeComponent();

            // Get the content manager from the application
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromTicks(333333);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;
       
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // Set the sharing mode of the graphics device to turn on XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);

            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);

            _grid = new Grid((int)(SharedGraphicsDeviceManager.DefaultBackBufferWidth ),(int) (SharedGraphicsDeviceManager.DefaultBackBufferHeight)-30,SharedGraphicsDeviceManager.Current.GraphicsDevice);

            if (null == elementRenderer)
            {
                elementRenderer = new UIElementRenderer(this, (int)SharedGraphicsDeviceManager.DefaultBackBufferWidth, (int)SharedGraphicsDeviceManager.DefaultBackBufferHeight);
            }

            // Start the timer
            timer.Start();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();

            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);

            base.OnNavigatedFrom(e);
        }

        /// <summary>
        /// Allows the page to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            // TODO: Add your update logic here
       
            TouchCollection touchCollection = TouchPanel.GetState();
            foreach (TouchLocation tl in touchCollection)
            {

                if (tl.State == TouchLocationState.Moved)
                {
                    if (tl.Position.Y <(int)SharedGraphicsDeviceManager.DefaultBackBufferHeight- 40)
                    {
                        _grid.SpawnSand(new Microsoft.Xna.Framework.Rectangle((int)(tl.Position.X * .25f) - (_size / 2), (int)(tl.Position.Y * .25f) - (_size / 2), _size, _size), _state);
                    }
                }

            }

            if (MainPage.FlushMode)
            {
                _grid.SpawnSand(new Microsoft.Xna.Framework.Rectangle(0, ((SharedGraphicsDeviceManager.DefaultBackBufferHeight / 4) -15), SharedGraphicsDeviceManager.DefaultBackBufferWidth / 4, 15), 0);
            }
            _grid.update();
        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            elementRenderer.Render();

            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.White);
            spriteBatch.Begin();
            _grid.DrawTextureData(spriteBatch);
            spriteBatch.Draw(elementRenderer.Texture, Vector2.Zero, Color.White);
          spriteBatch.End();
            // TODO: Add your drawing code here
        }

        private void Sand_Click(object sender, RoutedEventArgs e)
        {

            _state = 1;
        }

        private void Wall_Click(object sender, RoutedEventArgs e)
        {
            _state = 2;
        }
        private void Erase_Click(object sender, RoutedEventArgs e)
        {
            _state = 0;
        }

        private void BrushSize_Click(object sender, RoutedEventArgs e)
        {
            string SetString ="";
            switch (BrushSize.Content.ToString())
            {
                case "2X" :
                    SetString = "5X";
                    _size = 5;
                    break;
                case "5X":
                    SetString = "10X";
                    _size = 10;
                    break;
                case "10X":
                    SetString = "15X";
                    _size = 15;
                    break;
                case "15X":
                    SetString = "20X";
                    _size = 20;
                    break;
                case "20X":
                    SetString = "50X";
                    _size = 50;
                    break;
                case "50X":
                    SetString = "2X";
                    _size = 2;
                    break;
  
                default:
                    break;
            }
            BrushSize.Content = SetString;
        }

        private void Flush_Click(object sender, RoutedEventArgs e)
        {

            if (!MainPage.FlushMode)
                
                MainPage.FlushMode = true;
            else
                MainPage.FlushMode = false;
        }


    }
}